package com.hh.config;

import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.DefaultManagedTaskScheduler;
import org.springframework.scheduling.config.CronTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
// spring 用 ConcurrentTaskScheduler ,在afterPropertiesSet里面schedule task
@Component
public class 自定义动态Cron定时任务 implements SchedulingConfigurer {
    static int i = 0;
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        CronTask task = new CronTask(() -> System.out.println(i++), "0/1 * * * * ? ");
//        taskRegistrar.addCronTask(task);
    }

    public static void main(String[] args) {
        // 默认用了一个newSingleThreadScheduledExecutor，用trigger的nextExecutionTime执行一个ReschedulingRunnable的run方法
        new DefaultManagedTaskScheduler().schedule(() -> System.out.println(i++), new CronTrigger("0/1 * * * * ? "));

    }
}
